#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>

class Student{
    private:
    std::string name;
    int id;
    std::map<std::string,double> grades;

    public:
    Student(std::string n,int i)
    :name(n),id(i){
    }
    void addGrade(const std::string & subject,double grade)
    {
        grades[subject]=grade;
    }
    double getAverage() const
    {
        if(grades.empty())
        {
            return 0.0;
        }
        double sum=0.0;
        for(const auto& grade:grades)
        {
            sum+=grade.second;
        }
        return sum/grades.size();
    }
    void display() const
    {
        std::cout<<"Student:"<<name<<"(ID:)"<<id<<std::endl;
        std::cout<<"Grade:\n";
        for(const auto& pair:grades)
        {
            std::cout<<" "<<pair.first<<":"<<pair.second<<"\n";
        }
        std::cout<<"Average: "<<getAverage()<<"\n\n";
    }
    int getid() const
    {
        return id;
    }
    std::string getName() const
    {
        return name;
    }
};
class GradeManager{
    private:
    std::vector<Student> students;
    
    public:
    void addStudent(const Student& s)
    {
        students.push_back(s);
    }
     
    bool removeStudent(int id)
    {
        auto it=remove_if(students.begin(),students.end(),
        [id](const Student& s){return s.getid()==id;});
        if(it!=students.end())
        {
            students.erase(it,students.end());
            return true;
        }
        return false;
    }
     void displayAll() const {
        for (const auto& student : students) {
            student.display();
        }
    }
    Student* findStudent(int id) {
        auto it = std::find_if(students.begin(), students.end(),
            [id](const Student& s) { return s.getid() == id; });
        
        if (it != students.end()) {
            return &(*it);
        }
        return nullptr;
    }
};
int main() {
    GradeManager manager;
    
    // 添加学生
    Student s1("Alice", 1001);
    s1.addGrade("Math", 90.5);
    s1.addGrade("Physics", 85.0);
    manager.addStudent(s1);
    
    Student s2("Bob", 1002);
    s2.addGrade("Math", 78.0);
    s2.addGrade("Chemistry", 92.5);
    manager.addStudent(s2);
    
    // 显示所有学生
    std::cout << "All Students:\n";
    manager.displayAll();
    
    // 查找学生
    Student* found = manager.findStudent(1001);
    if (found) {
        std::cout << "Found student: " << found->getName() << "\n";
    }
    
    // 删除学生
    if (manager.removeStudent(1002)) {
        std::cout << "Student 1002 removed.\n";
    }
    
    std::cout << "\nRemaining Students:\n";
    manager.displayAll();
    
    return 0;
}